{
  "name": "E-commerce Order Fulfillment & Returns Management Agent",
  "nodes": [
    {
      "parameters": {
        "updates": [
          "message"
        ],
        "additionalFields": {}
      },
      "id": "c731efc1-ae71-4307-9f89-105707053ab2",
      "name": "Telegram Trigger",
      "type": "n8n-nodes-base.telegramTrigger",
      "typeVersion": 1.2,
      "position": [
        0,
        0
      ],
      "webhookId": "e69fd63a-4cf6-443e-ac3e-4d7bcb04f5d2",
      "credentials": {
        "telegramApi": {
          "id": "NFpPTqiHG1arwJ14",
          "name": "Telegram account"
        }
      }
    },
    {
      "parameters": {
        "assignments": {
          "assignments": [
            {
              "id": "id-1",
              "name": "customer_message",
              "value": "={{ $json.message.text }}",
              "type": "string"
            },
            {
              "id": "id-2",
              "name": "customer_name",
              "value": "={{ $json.message.from.first_name + ' ' + ($json.message.from.last_name || '') }}",
              "type": "string"
            },
            {
              "id": "id-3",
              "name": "chat_id",
              "value": "={{ $json.message.chat.id }}",
              "type": "string"
            }
          ]
        },
        "includeOtherFields": true,
        "options": {}
      },
      "id": "6cabe2c5-687f-443d-b463-ef8475690066",
      "name": "Workflow Configuration",
      "type": "n8n-nodes-base.set",
      "typeVersion": 3.4,
      "position": [
        224,
        0
      ]
    },
    {
      "parameters": {
        "promptType": "define",
        "text": "=Customer Name: {{ $json.customer_name }}\nCustomer Message: {{ $json.customer_message }}\nChat ID: {{ $json.chat_id }}",
        "options": {
          "systemMessage": "=You are an E-commerce Order Fulfillment and Returns Management Assistant.\n\nYour responsibilities:\n1. Identify whether the customer wants to INITIATE a return/refund or CHECK order/return status.\n2. For new return requests, collect:\n   - Order Number\n   - Date of Purchase\n   - Return Reason (e.g., Defective Product, Wrong Item, Size Issue, Changed Mind, etc.)\n   - Brief description\n3. Validate the order using the Order Master Database Tool and Extract the Customer_Name.\n4. If the order is valid and eligible for return:\n   - Generate a unique return reference ID in format: RET-YYYYMMDD-[last 4 digits of order]-HHmmss\n   - Register the return using Return Register Tool\n   - Send email confirmation using Gmail Tool\n   - Clearly explain return process and refund timeline\n   - Respond to the customer using Telegram Response Tool\n5. If the order is invalid:\n   - Politely explain the reason\n   - Do not register a return\n   - Respond using Telegram Response Tool\n6. For status checks:\n   - Ask for order or return reference number if missing\n   - Retrieve status using Order Status Tool\n   - Explain the current stage in simple language\n   - Provide expected delivery or refund timeline\n   - Respond using Telegram Response Tool\n\nStrict rules:\n- Do NOT make commitments outside company return policy.\n- Do NOT guarantee refund amounts without verification.\n- Be clear, calm, and professional.\n- Maintain a polite, empathetic tone - returns can be frustrating.\n- Avoid technical jargon.\n- Ask follow-up questions if information is missing.\n- ALWAYS use the Telegram Response Tool to send your final response to the customer."
        }
      },
      "id": "9d2cca22-884a-4892-b374-79c4980d95be",
      "name": "E-commerce Returns Agent",
      "type": "@n8n/n8n-nodes-langchain.agent",
      "typeVersion": 3,
      "position": [
        496,
        0
      ]
    },
    {
      "parameters": {
        "model": {
          "__rl": true,
          "mode": "id",
          "value": "gpt-4o"
        },
        "builtInTools": {},
        "options": {
          "temperature": 0.2
        }
      },
      "id": "04036e56-68a8-4050-ad6b-04a52098e891",
      "name": "OpenAI Chat Model",
      "type": "@n8n/n8n-nodes-langchain.lmChatOpenAi",
      "typeVersion": 1.3,
      "position": [
        320,
        224
      ],
      "credentials": {
        "openAiApi": {
          "id": "ICwxUBbatsF2sDvy",
          "name": "OpenAi account"
        }
      }
    },
    {
      "parameters": {
        "sessionIdType": "customKey",
        "sessionKey": "={{ $('Workflow Configuration').item.json.chat_id }}",
        "contextWindowLength": 10
      },
      "id": "2bb94d6d-2e68-4a5c-942c-c23477d2c0f7",
      "name": "Simple Memory",
      "type": "@n8n/n8n-nodes-langchain.memoryBufferWindow",
      "typeVersion": 1.3,
      "position": [
        480,
        224
      ]
    },
    {
      "parameters": {
        "descriptionType": "manual",
        "toolDescription": "Use this tool to validate order information. Provide the order number to check if the order exists, delivery status, and return eligibility. Returns order_status, product_details, and purchase_date.",
        "documentId": {
          "__rl": true,
          "value": "1RE1tAGxlnDxvY0Gd4z4MwWDEWB3lG8EIQGNU_kJDK7U",
          "mode": "list",
          "cachedResultName": "Order_Master_Database",
          "cachedResultUrl": "https://docs.google.com/spreadsheets/d/1RE1tAGxlnDxvY0Gd4z4MwWDEWB3lG8EIQGNU_kJDK7U/edit?usp=drivesdk"
        },
        "sheetName": {
          "__rl": true,
          "value": "gid=0",
          "mode": "list",
          "cachedResultName": "Sheet1",
          "cachedResultUrl": "https://docs.google.com/spreadsheets/d/1RE1tAGxlnDxvY0Gd4z4MwWDEWB3lG8EIQGNU_kJDK7U/edit#gid=0"
        },
        "options": {}
      },
      "id": "3102787d-3f1a-464f-979b-7a75d83d58c6",
      "name": "Order Master Database Tool",
      "type": "n8n-nodes-base.googleSheetsTool",
      "typeVersion": 4.7,
      "position": [
        624,
        256
      ],
      "credentials": {
        "googleSheetsOAuth2Api": {
          "id": "LOs2dbk9lby0NfDM",
          "name": "Google Sheets account"
        }
      }
    },
    {
      "parameters": {
        "descriptionType": "manual",
        "toolDescription": "=Use this tool to register a new return request. Provide return_reference_id, order_number, purchase_date, return_reason, description, and return_status (set to Initiated). This creates an official return record.",
        "operation": "append",
        "documentId": {
          "__rl": true,
          "value": "1tXApTtwNhXVoSE6fsxVhkqXGo403lmF8bABFOUg_d4c",
          "mode": "list",
          "cachedResultName": "Return_Register",
          "cachedResultUrl": "https://docs.google.com/spreadsheets/d/1tXApTtwNhXVoSE6fsxVhkqXGo403lmF8bABFOUg_d4c/edit?usp=drivesdk"
        },
        "sheetName": {
          "__rl": true,
          "value": "gid=0",
          "mode": "list",
          "cachedResultName": "Sheet1",
          "cachedResultUrl": "https://docs.google.com/spreadsheets/d/1tXApTtwNhXVoSE6fsxVhkqXGo403lmF8bABFOUg_d4c/edit#gid=0"
        },
        "columns": {
          "mappingMode": "defineBelow",
          "value": {
            "Return_Ref_ID": "={{ /*n8n-auto-generated-fromAI-override*/ $fromAI('Return_Ref_ID', ``, 'string') }}",
            "Order_Number": "={{ /*n8n-auto-generated-fromAI-override*/ $fromAI('Order_Number', ``, 'string') }}",
            "Customer_Name": "={{ /*n8n-auto-generated-fromAI-override*/ $fromAI('Customer_Name', ``, 'string') }}",
            "Purchase_Date": "={{ /*n8n-auto-generated-fromAI-override*/ $fromAI('Purchase_Date', ``, 'string') }}",
            "Return_Reason": "={{ /*n8n-auto-generated-fromAI-override*/ $fromAI('Return_Reason', ``, 'string') }}",
            "Description": "={{ /*n8n-auto-generated-fromAI-override*/ $fromAI('Description', ``, 'string') }}",
            "Return_Status": "={{ /*n8n-auto-generated-fromAI-override*/ $fromAI('Return_Status', ``, 'string') }}",
            "Created_At": "={{ $now.format('yyyy-MM-dd HH:mm:ss') }}"
          },
          "matchingColumns": [],
          "schema": [
            {
              "id": "Return_Ref_ID",
              "displayName": "Return_Ref_ID",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true
            },
            {
              "id": "Order_Number",
              "displayName": "Order_Number",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true
            },
            {
              "id": "Customer_Name",
              "displayName": "Customer_Name",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true
            },
            {
              "id": "Purchase_Date",
              "displayName": "Purchase_Date",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true
            },
            {
              "id": "Return_Reason",
              "displayName": "Return_Reason",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true
            },
            {
              "id": "Description",
              "displayName": "Description",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true
            },
            {
              "id": "Return_Status",
              "displayName": "Return_Status",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true
            },
            {
              "id": "Created_At",
              "displayName": "Created_At",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true
            }
          ],
          "attemptToConvertTypes": false,
          "convertFieldsToString": false
        },
        "options": {}
      },
      "id": "9cd9729e-6d1d-418e-bfec-8d25b2d74205",
      "name": "Return Register Tool",
      "type": "n8n-nodes-base.googleSheetsTool",
      "typeVersion": 4.7,
      "position": [
        784,
        256
      ],
      "credentials": {
        "googleSheetsOAuth2Api": {
          "id": "LOs2dbk9lby0NfDM",
          "name": "Google Sheets account"
        }
      }
    },
    {
      "parameters": {
        "descriptionType": "manual",
        "toolDescription": "Use this tool to check the status of an existing order or return. Provide the order_number or return_reference_id to retrieve current processing stage, last updated date, and delivery/refund status information.",
        "documentId": {
          "__rl": true,
          "value": "1jE3akckFoXRJ4WX_CXA360NrjToxGJ1atDFO4NESl6M",
          "mode": "list",
          "cachedResultName": "Order_Status",
          "cachedResultUrl": "https://docs.google.com/spreadsheets/d/1jE3akckFoXRJ4WX_CXA360NrjToxGJ1atDFO4NESl6M/edit?usp=drivesdk"
        },
        "sheetName": {
          "__rl": true,
          "value": "gid=0",
          "mode": "list",
          "cachedResultName": "Sheet1",
          "cachedResultUrl": "https://docs.google.com/spreadsheets/d/1jE3akckFoXRJ4WX_CXA360NrjToxGJ1atDFO4NESl6M/edit#gid=0"
        },
        "options": {}
      },
      "id": "1deb2a1f-6831-4a63-a496-81182bc33046",
      "name": "Order Status Tool",
      "type": "n8n-nodes-base.googleSheetsTool",
      "typeVersion": 4.7,
      "position": [
        928,
        320
      ],
      "credentials": {
        "googleSheetsOAuth2Api": {
          "id": "LOs2dbk9lby0NfDM",
          "name": "Google Sheets account"
        }
      }
    },
    {
      "parameters": {
        "descriptionType": "manual",
        "toolDescription": "Use this tool to send an official return acknowledgement email to the customer. Provide recipient email, return reference ID, order number, and return details including refund timeline.",
        "sendTo": "={{ $fromAI('customer_email', 'Customer email address', 'string') }}",
        "subject": "={{ $fromAI('email_subject', 'Email subject line', 'string', 'Return Request Confirmation') }}",
        "message": "={{ $fromAI('email_body', 'Email body content with return details', 'string') }}",
        "options": {}
      },
      "id": "c736cbfc-6c4b-458d-b4c4-0ffcd386101b",
      "name": "Gmail Tool",
      "type": "n8n-nodes-base.gmailTool",
      "typeVersion": 2.1,
      "position": [
        1072,
        224
      ],
      "webhookId": "15cba1c5-81ac-4d4d-9a13-35fc51ad3bbe",
      "credentials": {
        "gmailOAuth2": {
          "id": "cyqCGWcggZNMcSOv",
          "name": "Gmail account"
        }
      }
    },
    {
      "parameters": {
        "chatId": "={{ $('Telegram Trigger').item.json.message.chat.id }}",
        "text": "={{ $json.output }}",
        "additionalFields": {}
      },
      "type": "n8n-nodes-base.telegram",
      "typeVersion": 1.2,
      "position": [
        928,
        0
      ],
      "id": "f80926a8-64c7-4a26-bcd1-69ce6ccb5cbf",
      "name": "Send a text message",
      "webhookId": "3c111795-fe7d-497d-b853-a6432330a256",
      "credentials": {
        "telegramApi": {
          "id": "NFpPTqiHG1arwJ14",
          "name": "Telegram account"
        }
      }
    }
  ],
  "pinData": {},
  "connections": {
    "Telegram Trigger": {
      "main": [
        [
          {
            "node": "Workflow Configuration",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Workflow Configuration": {
      "main": [
        [
          {
            "node": "E-commerce Returns Agent",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "OpenAI Chat Model": {
      "ai_languageModel": [
        [
          {
            "node": "E-commerce Returns Agent",
            "type": "ai_languageModel",
            "index": 0
          }
        ]
      ]
    },
    "Simple Memory": {
      "ai_memory": [
        [
          {
            "node": "E-commerce Returns Agent",
            "type": "ai_memory",
            "index": 0
          }
        ]
      ]
    },
    "Order Master Database Tool": {
      "ai_tool": [
        [
          {
            "node": "E-commerce Returns Agent",
            "type": "ai_tool",
            "index": 0
          }
        ]
      ]
    },
    "Return Register Tool": {
      "ai_tool": [
        [
          {
            "node": "E-commerce Returns Agent",
            "type": "ai_tool",
            "index": 0
          }
        ]
      ]
    },
    "Order Status Tool": {
      "ai_tool": [
        [
          {
            "node": "E-commerce Returns Agent",
            "type": "ai_tool",
            "index": 0
          }
        ]
      ]
    },
    "Gmail Tool": {
      "ai_tool": [
        [
          {
            "node": "E-commerce Returns Agent",
            "type": "ai_tool",
            "index": 0
          }
        ]
      ]
    },
    "E-commerce Returns Agent": {
      "main": [
        [
          {
            "node": "Send a text message",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "active": true,
  "settings": {
    "executionOrder": "v1"
  },
  "versionId": "84cf0ee1-9598-4949-8cd7-193018aaf2c1",
  "meta": {
    "instanceId": "3a43da28588548e21903e71cf1dc3ddd65c24bf0c62e7e4b77542ffe87ad79c6"
  },
  "id": "F0cDNNnRriy3ZmNf",
  "tags": []
}